import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../requests/index.js";
Page({

 
  data: {
    leftMenuList:[],
    rightContent:[],
    currentIndex:0,
    scrollTop:0
  },
  Cates:[],


  onLoad: function (options) {
    // this.getCates();
    const Cates = wx.getStorageSync("cates");
    if(!Cates){
      this.getCates();
    }else{
      if(Date.now()-Cates.time>1000*10){
        this.getCates();
      }else{
        this.Cates = Cates.data;
        let leftMenuList = this.Cates.map(v=>v.cat_name)
        let rightContent = this.Cates[0].children;
        this.setData({
          leftMenuList,
          rightContent
        })
      }
    }
      
    
  },
  async getCates(){
    // request({url:"/categories"})
    // .then(result =>{
    //     this.Cates=result.data.message;
    //     wx.setStorageSync("cates", {time:Date.now(),data:this.Cates});
          
    //     let leftMenuList = this.Cates.map(v=>v.cat_name)
    //     let rightContent = this.Cates[0].children;
    //     this.setData({
    //       leftMenuList,
    //       rightContent
    //     })
      
    // })
    const res = await request({url:"/categories"});
      this.Cates=res;
      wx.setStorageSync("cates", {time:Date.now(),data:this.Cates});
        
      let leftMenuList = this.Cates.map(v=>v.cat_name)
      let rightContent = this.Cates[0].children;
      this.setData({
        leftMenuList,
        rightContent
      })
  },

  handleItemTap(e){
    let index = e.currentTarget.dataset.index;
    let rightContent = this.Cates[index].children;
    this.setData({
      rightContent,
      currentIndex:index,
      scrollTop:0
    })
  }

  
})